const { get, post, put } = require('../../common/ajax');
import { ORDER_URL, BRD_BRAND_URL } from '../../constants/url';

/**
 * 创建订单
 * @param {Object} params 订单参数
 */
export const createOrder = async (params) => {
    const res = await post(ORDER_URL.create, {
        ...params
    });
    return res.data ? true : false;
};

/**
 * 创建订单并支付
 * @param {Object} params 订单生成参数
 */
export const createAndPayOrder = async (params) => {
    const res = await post(ORDER_URL.createAndPay, {
        ...params
    });
    return res.data || null;
};

/**
 * 支付订单
 * @param {String|Number} orderNumber 订单号
 * @param {Object} params 支付参数
 */
export const payOrder = async (orderNumber, params) => {
    const res = await post(ORDER_URL.pay(orderNumber), {
        ...params,
    });
    return res.data || null;
};

/**
 * 根据订单类型分页获取订单数据
 * @param {String|Number} orderType 订单类型
 * @param {Object} params 查询参数
 */
export const getOrderList = async (params) => {
    const res = await get(ORDER_URL.list, { ...params });
    return res.data.rows || [];
};

/**
 * 根据订单号获取订单详情
 * @param {Number|String} orderNumber 订单号
 */
export const getOrderDetail = async (orderNumber) => {
    const res = await get(ORDER_URL.detail(orderNumber));
    return res.data || {};
};

/**
 * 取消订单
 * @param {String|Number} orderNumber 订单号
 */
export const cancelOrder = async (orderNumber) => {
    const res = await put(ORDER_URL.cancel(orderNumber));
    return res.data ? true : false;
};

/**
 * 确认收货
 * @param {String|Number} orderNumber 订单号
 */
export const confirmExpressOrder = async (orderNumber) => {
    const res = await put(ORDER_URL.confirmExpress(orderNumber));
    return res.data ? true : false;
};

/**
 * 确认订单
 * @param {String|Number} orderNumber 订单号
 */
export const confirmPickupOrder = async (orderNumber) => {
    const res = await put(ORDER_URL.confirmPickup(orderNumber));
    return res.data ? true : false;
};

/**
 * 创建售后单
 * @param {Object} params 售后单参数
 */
export const createAfterSaleOrder = async (params) => {
    const res = await post(ORDER_URL.afterSaleApply, {
        ...params
    });
    return res.data || null;
};

/**
 * 获取售后列表
 * @param {Object} params 查询参数
 */
export const getAfterSaleList = async (params) => {
    const res = await get(ORDER_URL.afterSaleList, params);
    return res.data.rows || [];
};

/**
 * 获取售后单详情
 * @param {Number|String} orderNumber 订单号
 */
export const getAfterSaleOrderDetail = async (orderNumber) => {
    const res = await get(ORDER_URL.afterSaleDetail(orderNumber));
    return res.data || null;
};

/**
 * 添加快递信息
 * @param {String|Number} orderNumber 订单号
 * @param {Object} expressInfo 快递信息
 */
export const addExpressInfo = async (orderNumber, expressInfo) => {
    const res = await put(ORDER_URL.afterSaleExpressUpdate(orderNumber), {
        ...expressInfo,
    });
    return res.data ? true : false;
};

/**
 * 取消售后单
 * @param {String|Number} orderNumber 订单号
 */
export const cancelAfterSaleOrder = async (orderNumber) => {
    const res = await put(ORDER_URL.afterSaleCancel(orderNumber));
    return res.data ? true : false;
};

/**
 * 获取品牌订单列表
 * @param {Object} params 查询参数
 */
export const getBrandOrderList = async (params) => {
    const res = await get(BRD_BRAND_URL.orderList(), { ...params });
    return res.data.rows || [];
};

/**
 * 获取订单详情
 * @param {String} orderNumber 订单号
 */
export const getBrandOrderDetail = async (orderNumber) => {
    const res = await get(BRD_BRAND_URL.orderDetail(orderNumber));
    return res.data || null;
};

/**
 * 品牌确认发货
 * @param {String|Number} orderNumber 订单号
 * @param {Object} param 参数
 */
export const deliverBrandOrder = async (orderNumber, param) => {
    const res = await put(BRD_BRAND_URL.orderDeliver(orderNumber), { ...param });
    return res.data ? true : false;
};

/**
 * 确认备货
 * @param {String|Number} orderNumber 订单号
 */
export const prepareBrandOrder = async (orderNumber) => {
    const res = await put(BRD_BRAND_URL.orderPrepare(orderNumber));
    return res.data ? true : false;
};

/**
 * 确认提货
 * @param {String|Number} orderNumber 订单号
 * @param {String} code 自提码
 */
export const pickBrandOrder = async (orderNumber, code) => {
    const param = {
        pickupCode: code,
    };
    const res = await put(BRD_BRAND_URL.orderPick(orderNumber), param);
    return res.data ? {
        flag: true,
        msg: '',
    } : { flag: false,
        msg: res.msg,
    };
};

/**
 * 获取品牌售后订单列表
 */
export const getBrandAfterSaleOrderList = async (params) => {
    const res = await get(BRD_BRAND_URL.afterSaleOrderList(), { ...params });
    return res.data.rows || [];
};

/**
 * 品牌侧获取售后单详情
 * @param {String|Number} orderNumber 售后单号
 */
export const getBrandAfterSaleOrderDetail = async (orderNumber) => {
    const res = await get(BRD_BRAND_URL.afterSaleOrderDetail(orderNumber));
    return res.data || {};
};

/**
 * 品牌侧处理售后单
 * @param {String|Number} orderNumber 售后单号
 * @param {Object} params 处理参数
 */
export const dealBrandAfterSaleOrder = async (orderNumber, params) => {
    const result = await put(BRD_BRAND_URL.dealAfterSaleOrder(orderNumber), params);
    return result.data ? true : false;
};

/**
 * 同意入库
 * @param {String} orderNumber 售后单号
 */
export const brandAgreeWearhouse = async (orderNumber) => {
    const res = await put(BRD_BRAND_URL.warehouseAgree(orderNumber));
    return res.data ? true : false;
};

/**
 * 拒绝入库
 * @param {String} orderNumber 售后单号
 * @param {Object} params 参数
 */
export const brandRefuseWearhouse = async (orderNumber, params) => {
    const res = await put(BRD_BRAND_URL.warehouseRefuse(orderNumber), { ...params });
    return res.data ? true : false;
};

/**
 * 获取店铺订单列表
 * @param {Object} params 查询参数
 */
 export const getStoreOrderList = async (params) => {
    const res = await get(ORDER_URL.storeOrderList(), { ...params });
    return res.data ? res.data.rows : [];
};

/**
 * 获取店铺订单详情
 * @param {String} orderNumber 订单号
 * @param {String|Number} storeId 店铺id
 */
export const getStoreOrderDetail = async (orderNumber, storeId) => {
    const res = await get(ORDER_URL.storeOrderDetail(orderNumber, storeId));
    return res.data || null;
};

/**
 * 获取店铺售后单列表
 * @param {Object} params 查询参数
 */
export const getStoreAfterSaleList = async (params) => {
    const res = await get(ORDER_URL.storeAfterSaleList(), { ...params });
    return res.data ? res.data.rows : [];
};

/**
 * 获取店铺售后单详情
 * @param {String} orderNumber 订单号
 * @param {String|Number} storeId 店铺id
 */
export const getStoreAfterSaleDetail = async (orderNumber, storeId) => {
    const res = await get(ORDER_URL.storeAfterSaleDetail(orderNumber, storeId));
    return res.data || null;
};

/**
 * 获取推广订单列表
 * @param {Object} params 查询参数
 */
export const getExpandOrderList = async (params) => {
    const res = await get(ORDER_URL.expandOrderList, { ...params });
    return res.data ? res.data.rows : [];
};

/**
 * 获取推广订单详情
 * @param {String} orderNumber 订单号
 * @param {String|Number} spnId 代言人id
 */
export const getExpandOrderDetail = async (orderNumber, spnId) => {
    const res = await get(ORDER_URL.expandOrderDetail(orderNumber, spnId));
    return res.data || null;
};

/**
 * 获取推广售后单列表
 * @param {Object} params 查询参数
 */
export const getExpandAfterSaleList = async (params) => {
    const res = await get(ORDER_URL.expandAfterSaleList, { ...params });
    return res.data ? res.data.rows : [];
};

/**
 * 获取推广售后单详情
 * @param {String} orderNumber 订单号
 * @param {String|Number} spnId 代言人id
 */
export const getExpandAfterSaleDetail = async (orderNumber, spnId) => {
    const res = await get(ORDER_URL.expandAfterSaleDetail(orderNumber, spnId));
    return res.data || null;
};

/**
 * 获取品牌订单过滤条件
 * @param {Object} params 参数
 */
export const getBrandOrderFilter = async (params) => {
    const res = await get(ORDER_URL.brandOrderFilter(), { ...params });
    return res.data ? res.data.rows : [];
};

/**
 * 获取店铺订单过滤条件
 * @param {Object} params 参数
 */
export const getStoreOrderFilter = async (params) => {
    const res = await get(ORDER_URL.storeOrderFilter(), { ...params });
    return res.data ? res.data.rows : [];
};

/**
 * 获取推广订单过滤条件
 * @param {Object} params 参数
 */
export const getExpandOrderFilter = async (params) => {
    const res = await get(ORDER_URL.expandOrderFilter, { ...params });
    return res.data ? res.data.rows : [];
};